package com.example.todolist.view

import android.util.Log
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.example.todolist.util.LoggerTag

class ItemTouchHelperCallback(
    private val adapter: ItemTouchHelperAdapter,
    private val onItemMoved: (Int, Int) -> Unit
) :
    ItemTouchHelper.Callback() {
    private var startDrag = -1
    private var endDrag = -1

    // 指定拖拽和滑动支持方向
    override fun getMovementFlags(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder
    ): Int {
        if (adapter.getIsDelMode()) {
            var dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
            var swipeFlags = ItemTouchHelper.START or ItemTouchHelper.END

            return makeMovementFlags(dragFlags, 0)
        } else {
            return makeMovementFlags(0, 0)
        }
    }


    /**
     *  item被拖动的调用
     */
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        val fromPosition = viewHolder.adapterPosition
        val toPosition = target.adapterPosition
        // 这个方法拖拽每经过一次其他item都会调用
        Log.d(
            LoggerTag.MAIN_TAG,
            "onMove: 正在拖拽！！！start:${viewHolder.adapterPosition},target:${target.adapterPosition}"
        )
        adapter.onItemMove(fromPosition, toPosition)
//        endDrag = target.adapterPosition
//        Log.d(LoggerTag.MAIN_TAG, "onMove: 在变化！！！$endDrag")
        return true

    }

    // item被滑动删除时
    override fun onSwiped(
        viewHolder: RecyclerView.ViewHolder,
        direction: Int
    ) {
        TODO("Not yet implemented")
    }

    override fun isItemViewSwipeEnabled(): Boolean {
        // 是否启用滑动删除，默认true
        return false
    }

    override fun isLongPressDragEnabled() = true

    // 判断拖拽是否结束
    override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
        super.onSelectedChanged(viewHolder, actionState)
        Log.d(LoggerTag.MAIN_TAG, "onSelectedChanged: 进来了！！！start:$startDrag,end:$endDrag")
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            startDrag = viewHolder?.adapterPosition ?: -1
//            endDrag = startDrag
        }
//        if (startDrag != -1 && actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
//            Log.d(LoggerTag.MAIN_TAG, "onSelectedChanged: 要结束了！！！")
//            if (endDrag != startDrag) {
//                onItemMoved(startDrag, endDrag)
//                Log.d(LoggerTag.MAIN_TAG, "onSelectedChanged: 要改变了！！！")
//            }
//            // 位置重置
//            startDrag = -1
//            endDrag = -1
//        }
    }

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        endDrag = viewHolder.adapterPosition
        Log.d(LoggerTag.MAIN_TAG, "clearView: 拖拽结束了！！！start:$startDrag,end:$endDrag")
        onItemMoved(startDrag, endDrag)
        // 想办法把这个start和end==>结合currentList===>获取两个order1，order2===>数据库进行分类交换

    }
}